<?php
class ImgMapper{
    protected $table;
    protected $query;

    public function setTable($_table){
        $this->table = $_table;
        $this->query = 'SET NAMES utf8; SET CHARACTER SET utf8';
        return $this;
    }
    public function getTable(){
        return $this->table;
    }

    public function save($img){
        $arr = array(
            'text'      => $img->getText(),
            'user_id'   => $img->getUserid(),
            'msg_id'    => $img->getMessageid(),
            'date'      => $img->getDate()
        );
        $db = Database::getInstance();

        if(null == ($id = $img->getId())){
            $db->insert('comments', $arr);
        }else{
            $db->update('comments', $arr, $id);
        }
    }

    public function find($id, $img){
        $db = Database::getInstance();
        $data = $db->select('imgs', $id);
        $img    ->setText($data['text'])
                ->setUserid($data['user_id'])
                ->setName($data['name'])
                ->setMessageid($data['msg_id'])
                ->setId($id)
                ->setMapper($this);
    }

    public function delete($img){
        $db = Database::getInstance();;
        $db->delete('imgs', $img->getId());
    }

    static function countOfImg($str){
        $db = Database::getInstance();
        //$curuser = UserMapper::getCurrentUser();
        $countarr = $db->selectWhere('imgs', 'COUNT(id)', $str);
        //var_dump($countarr);
        //exit;
        return $countarr[0]['COUNT(id)'];
    }


    public function fetchAllImgs($msg_id){
        $db = Database::getInstance();
        $resultSet = $db->fetchAll('imgs WHERE msg_id = ' . $msg_id);
        $imgs = array();
        //$messages = array();
        foreach($resultSet as $one){
            $img = new ImgModel();
            $img    ->setText($one['text'])
                    ->setName($one['name'])
                    ->setUserid($one['user_id'])
                    ->setMessageid($one['msg_id'])
                    ->setId($one['id'])
                    ->setMapper($this);
            $imgs[] = $img;
        }
        return $imgs;
    }

    public function fetchAllImgsById($m_id){
        $db = Database::getInstance();
        $resultSet = $db->fetchAll('imgs WHERE msg_id = ' . $m_id);

        $imgs = array();
        $messages = array();
        if(!empty($resultSet)){
            foreach($resultSet as $one){
                $img = new ImgModel();
                $img = $img->find($one['id']);
                $imgs[] = $img;
            }
        }

        return $imgs;
    }

    public function fetchAllImgsByUserId($u_id){
        $db = Database::getInstance();
        $resultSet = $db->fetchAll('imgs WHERE user_id = ' . $u_id);

        $imgs = array();
        $messages = array();
        if(!empty($resultSet)){
            foreach($resultSet as $one){
                $img = new ImgModel();
                $img = $img->find($one['id']);
                $imgs[] = $img;
            }
        }

        return $imgs;
    }
}
?>
